/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrame.java
 *
 * Created on 11/11/2009, 16:18:34
 */

package siii;

import Conector.Conector;
import java.awt.BorderLayout;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class LoginFuncionario extends javax.swing.JFrame {

    private Conector conector = new Conector();
    private boolean flagApelido = false;
    private boolean flagSenha = false;
    private String loginInterno = "M";
    private String senhaInterna = "m";

    private MenuPadrao menuPadrao;
    /** Creates new form NewJFrame */
    public LoginFuncionario() {
        initComponents();
        validaApelido.setVisible(false);
        validaSenha.setVisible(false);
        BorderLayout layout = new BorderLayout();
        this.setLayout(layout);
        setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelSenha = new javax.swing.JLabel();
        funcSenha = new javax.swing.JPasswordField();
        jButtonEntrar = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();
        jLabelApelido = new javax.swing.JLabel();
        validaSenha = new javax.swing.JLabel();
        jLabelNomePesqueiro = new javax.swing.JLabel();
        jLabelInfo = new javax.swing.JLabel();
        validaApelido = new javax.swing.JLabel();
        funcApelido = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabelSenha.setText("Senha");

        funcSenha.setName("Senha"); // NOI18N
        funcSenha.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                funcSenhaFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                funcSenhaFocusLost(evt);
            }
        });
        funcSenha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                funcSenhaKeyPressed(evt);
            }
        });

        jButtonEntrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/salvar.png"))); // NOI18N
        jButtonEntrar.setText("Entrar");
        jButtonEntrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEntrarActionPerformed(evt);
            }
        });

        jButtonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/peixe_cancelar3.png"))); // NOI18N
        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jLabelApelido.setText("Apelido");

        validaSenha.setForeground(new java.awt.Color(255, 0, 0));
        validaSenha.setText("Senha Inválida");

        jLabelNomePesqueiro.setFont(new java.awt.Font("French Script MT", 1, 36));
        jLabelNomePesqueiro.setText("Pesqueiro do Marco");

        jLabelInfo.setFont(new java.awt.Font("Coronet", 0, 14));
        jLabelInfo.setForeground(new java.awt.Color(102, 153, 0));
        jLabelInfo.setText("Sistema de Automação de Pesqueiro");

        validaApelido.setForeground(new java.awt.Color(255, 0, 0));
        validaApelido.setText("Apelido Inválido");

        funcApelido.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                funcApelidoFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                funcApelidoFocusLost(evt);
            }
        });
        funcApelido.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                funcApelidoKeyPressed(evt);
            }
        });

        jLabel13.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/funcionario_login.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabelSenha)
                    .addComponent(jLabelApelido))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(funcApelido)
                    .addComponent(funcSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(validaApelido)
                    .addComponent(validaSenha))
                .addContainerGap(38, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(63, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabelInfo))
                    .addComponent(jLabelNomePesqueiro))
                .addGap(70, 70, 70))
            .addGroup(layout.createSequentialGroup()
                .addGap(110, 110, 110)
                .addComponent(jButtonEntrar, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonCancelar)
                .addContainerGap(48, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelNomePesqueiro)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel13))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelApelido)
                            .addComponent(validaApelido)
                            .addComponent(funcApelido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(funcSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelSenha)
                            .addComponent(validaSenha))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonEntrar)
                    .addComponent(jButtonCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void funcSenhaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_funcSenhaFocusGained
        validaSenha.setVisible(false);// TODO add your handling code here:
    }//GEN-LAST:event_funcSenhaFocusGained

    private void funcSenhaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_funcSenhaFocusLost
        try {
            if((funcSenha.getText()).compareTo(senhaInterna)==0)
            {
                flagSenha = true;
            }
            else
            {
                if(conector.consultaSenhaLogIn(funcSenha.getText()))
                    flagSenha = true;
                else
                    validaSenha.setVisible(true);
            }
            
        } catch (SQLException ex) {
            conector = new Conector();
            Logger.getLogger(LoginFuncionario.class.getName()).log(Level.SEVERE, null, ex);
        }
                
    }//GEN-LAST:event_funcSenhaFocusLost

    public void entrar() {

        if(flagApelido && flagSenha) {
            menuPadrao = new MenuPadrao(this.conector);
            this.setVisible(false);
        }
        else {
            validaApelido.setVisible(true);
            validaSenha.setVisible(true);
        }
    }
    private void jButtonEntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEntrarActionPerformed
        // TODO add your handling code here:

        if(flagApelido && flagSenha) {
            menuPadrao = new MenuPadrao(this.conector);
            this.setVisible(false);
        }
        else {
            validaApelido.setVisible(true);
            validaSenha.setVisible(true);
        }

    }//GEN-LAST:event_jButtonEntrarActionPerformed

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButtonCancelarActionPerformed

    private void funcApelidoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_funcApelidoFocusLost
        // TODO add your handling code here:
        try {
            if((funcApelido.getText()).compareTo(loginInterno)==0)
            {
                flagApelido = true;
            }
            else
            {
                if(conector.consultaApelidoLogIn(funcApelido.getText()))
                    flagApelido = true;
                else
                    validaApelido.setVisible(true);
            }
        } catch (SQLException ex) {
            Logger.getLogger(LoginFuncionario.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_funcApelidoFocusLost

    private void funcApelidoFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_funcApelidoFocusGained
        // TODO add your handling code here:
        this.validaApelido.setVisible(false);
    }//GEN-LAST:event_funcApelidoFocusGained

    private void funcSenhaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_funcSenhaKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER){

            try {
                if((funcSenha.getText()).compareTo(senhaInterna)==0)
                {
                    flagSenha = true;
                }
                else
                {
                    if(conector.consultaSenhaLogIn(funcSenha.getText()))
                        flagSenha = true;
                    else
                        validaSenha.setVisible(true);
                }
        } catch (SQLException ex) {
            conector = new Conector();
            Logger.getLogger(LoginFuncionario.class.getName()).log(Level.SEVERE, null, ex);
        }
            this.entrar();
        }
    }//GEN-LAST:event_funcSenhaKeyPressed

    private void funcApelidoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_funcApelidoKeyPressed
        // TODO add your handling code here:
                if(evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER){

            try {
                if((funcApelido.getText()).compareTo(loginInterno)==0)
                {
                    flagApelido = true;
                }
                else
                {
                    if(conector.consultaApelidoLogIn(funcApelido.getText()))
                        flagApelido = true;
                    else
                        validaSenha.setVisible(true);
                }
        } catch (SQLException ex) {
            conector = new Conector();
            Logger.getLogger(LoginFuncionario.class.getName()).log(Level.SEVERE, null, ex);
        }
            this.entrar();
        }

    }//GEN-LAST:event_funcApelidoKeyPressed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(LoginFuncionario.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(LoginFuncionario.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(LoginFuncionario.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(LoginFuncionario.class.getName()).log(Level.SEVERE, null, ex);
                }
                LoginFuncionario login = new LoginFuncionario();
                login.setVisible(true);



            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField funcApelido;
    private javax.swing.JPasswordField funcSenha;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonEntrar;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabelApelido;
    private javax.swing.JLabel jLabelInfo;
    private javax.swing.JLabel jLabelNomePesqueiro;
    private javax.swing.JLabel jLabelSenha;
    private javax.swing.JLabel validaApelido;
    private javax.swing.JLabel validaSenha;
    // End of variables declaration//GEN-END:variables

}
